<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\index\controller\Form;
use think\helper\Time;
use think\Db;
/**
 *
 *外来务工
 * @icon fa fa-circle-o
 */
class QuesMigrant extends Ques
{

    /**
     * QuesTeacher模型对象
     * @var \app\admin\model\QuesTeacher
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    $this->model = new \app\admin\model\QuesTeacher;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {
        $groupIds = $this->auth->getGroupIds();
        $whereQues = [];
        $whereTea = [];


        if(isset($this -> sess['teacherIds'])){
            $whereQues['teacher_id'] = ['in',$this -> sess['teacherIds']];
            $whereTea['id'] = ['in',$this -> sess['teacherIds']];
        }

        if (input('?title') && input('?answer'))
        {
            $title = input('title');
            $answer =  input('answer');
            $whereQues[$title] = $answer;
        }


        $whereRep = [];
        $groups = $this -> auth -> getGroupIds();
        if(in_array(6,$groups)){
            $filter = json_decode(input('filter'),true);
            if($filter['update_time']){
                $timeArr = explode(' - ',$filter['update_time']);
                if(isset($timeArr[1])){
                    $timeArr = [
                        strtotime($timeArr[0]),strtotime($timeArr[1])
                    ];
                }else{
                    $timeArr = Time::today();
                }
            }else{
                $timeArr = Time::today();
            }
            $college_ids = (new \app\admin\model\CollegeReport) -> where('type',2) -> whereBetween('create_time',$timeArr) -> column('college_id');

            $whereRep['college_id'] = ['in',$college_ids];
        }
        //学院单位
        if($groupIds[0]==7)
        {
            $info = (new \app\admin\model\College) -> where('name',$this->auth->username) -> find();
            if(isset($info))
            {
                $whereQues['college_id'] = $info['id'];
            }
        }

        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                -> where($this -> whereType)
                -> where($whereRep)
                -> where($whereQues)
                ->where($where)
                ->order($sort, $order)
                ->group('teacher_id')
                ->count();

            $list = $this->model
                -> where($this -> whereType)
                -> where($whereRep)
                -> where($whereQues)
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->group('teacher_id')
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }

        //学院单位
        if($groupIds[0]==7)
        {
            $info = (new \app\admin\model\College) -> where('name',$this->auth->username) -> find();
            if(isset($info))
            {
                $departmentId = (new \app\admin\model\Department) -> where('college_id',$info['id']) -> column('id');
                $whereTea['department_id'] = ['in',$departmentId];
            }
        }
        $allcount = (new \app\admin\model\Teacher()) -> where($this -> whereType) -> where($whereTea) -> count();
        $reportedcount = (new \app\admin\model\QuesTeacher()) -> where($this -> whereType) -> where($whereQues) -> whereBetween('add_time',Time::today()) -> group('teacher_id') -> count();

        $notreportedallcount = $allcount - $reportedcount;

        $today = Time::today();
        $co_model = new  \app\admin\model\CollegeReport;

        $co_row = $co_model ->where(['type'=>5,'college_id'=>$this->auth->gid])-> whereTime('report_time','between',$today)->find();
        if($co_row)
            $this -> assign('exist', 1);
        else
            $this -> assign('exist', 0);
        $todayStr = date("Y-m-d H:i:s",$today[0]).' - '.date("Y-m-d H:i:s",$today[1]);
        $this -> assignConfig('today', $todayStr);
        $this -> assign('allcount',$allcount);
        $this -> assign('reportedcount',$reportedcount);
        $this -> assign('notreportedallcount',$notreportedallcount);

        return $this->view->fetch();
    }

    public function exp(){
        $and = ' ';
        $gid = $this->auth->gid;
        //学院
        if($this->auth->gtypeid == 1){
            $and .= " and college_id = '$gid' ";
        }
        //班级
        if($this->auth->gtypeid == 3){
            exit('没有权限!');
        }

        $create_time = input('create_time');
        if(empty($create_time)){
            $timestampArr = Time::today();
            $starttime = strtotime($timestampArr[0]);
            $endtime = strtotime($timestampArr[1]);

        }else{
            $timeArr = explode(' - ',$create_time);
            $starttime = strtotime($timeArr[0]);
            $endtime = strtotime($timeArr[1]);
        }

        $province = input('province');
        $city = input('city');



        if(!empty($starttime)){
            $and .= " and create_time >='$starttime' ";
        }
        if(!empty($endtime)){
            $and .= " and create_time <= '$endtime' " ;
        }

        if(!empty($province)){
            $and .= " and province='$province' ";
        }
        if(!empty($city)){
            $and .= " and city='$city' ";
        }
        if($this -> whereType){
            $and .= " and type=".$this -> whereType['type'];
        }

        // $sql = "select nickname,tel,classes_name,college_name,province,city,district, wx_formatted_address,inoculation,
        //     case a_1 when '0' then '无情况'  when '1' then '途径武汉' when '2' then '途径湖北(除武汉)' when '3' then '居住武汉' when '4' then '居住湖北(除武汉)' when '5' then '接触武汉籍人员'  when '6' then '接触湖北(除武汉)籍人员' when '7' then '从外省返鲁' when '8' then '在境外停留或途经' else '-' end a_1,
        //     case a_1_1 when 0 then '无' else from_unixtime(a_1_1, '%Y-%m-%d') end a_1_1,
        //     case a_2 when '0' then '无情况'  when '1' then '接触确诊病历' when '2' then '接触疑似病历' when '3' then '接触无症状感染者' else '-' end a_2,
        //     case a_2_1 when 0 then '无' else from_unixtime(a_2_1, '%Y-%m-%d') end a_2_1,
        //     case a_3 when '0' then '无' when '1' then '医院隔离' when '2' then '当地指定点隔离' when '3' then '居家隔离' else '-' end a_3,
        //     case a_3_1 when 0 then '无' else from_unixtime(a_3_1, '%Y-%m-%d') end a_3_1,
        //     case a_3_2 when '0' then '否' when '1' then '是' else '-' end a_3_2,
        //     case a_4 when '0' then '健康'  when '1' then '咳嗽' when '2' then '发烧' when '3' then '呼吸困难等' when '4' then '确诊病历' when '5' then '疑似病历'  when '6' then '其它' when '7' then '咽痛、乏力、肌肉酸痛、胸闷、恶心呕吐、腹泻、结膜炎' else '-' end a_4,
        //     a_4_1,
        //     case a_5 when '0' then '无情况'  when '1' then '接触确诊病历' when '2' then '接触疑似病历' when '3' then '确诊病历' when '4' then '疑似病历' when '5' then '有境外返回' else '-' end a_5,
        //     case a_5_1 when 0 then '无' else from_unixtime(a_5_1, '%Y-%m-%d') end a_5_1,
        //     case a_6 when '0' then '未返程' when '1' then '返程' else '-' end a_6,
        //     case a_6_1 when '0' then '自驾' when '1' then '火车' when '2' then '飞机' when '3' then '长途车' else '-' end a_6_1,
        //     a_6_2,
        //     case a_7 when '0' then '没有' when '1' then '因值班进入' when '2' then '因其他事宜进入' else '-' end a_7,
        //     case a_9 when '0' then '36度以下' when '1' then '36-36.5度' when '2' then '36.6-36.9度' when '3' then '37-37.2度' when '4' then '37.3-37.9度' when '5' then '38-38.5度' when '6' then '38.6-38.9度' when '7' then '39-40度' when '8' then '40度以上' end a_9,
        //     case create_time when 0 then '无' else from_unixtime(create_time, '%Y-%m-%d') end create_time
        //     from qa_ques_teacher where 1=1  $and";

        // echo $sql;exit();
        // $rows = Db::query($sql);
        $rows = (new \app\admin\model\QuesTeacher()) -> where("1=1 $and") -> select();

        $fp = fopen('php://output', 'a+');
        header("Content-Type:application/vnd.ms-excel;charset=utf-8");
        header("Content-Disposition:filename=教职工上报数据导出.csv");

        // $title = array('序号','姓名','手机号','部门','单位','省份','城市', '区县',
        //     '微信定位地址','你最近是否有下属情况','填写时间','近期接触相关人员情况','接触病例时间','是否医学观察',
        //     '医学观察时间','是否结束隔离','你本人身体近况','身体状况说明','家庭成员近期','家庭成员接触病例时间','是否有返程计划','返程工具','车次','今日有无进入校园','体温范围','疫苗接种情况','上报日期');
        // $title = array('序号','姓名','部门','手机号','单位','省份','城市', '区县','微信定位地址','具体行程(上午)','具体行程(下午)','具体行程(晚上)','本人或共同居住人是否有下列情况','本人或共同居住人是否有下列情况','所在社区（村居）21天内发生疫情','14天内本人或家庭成员有疫情重点地区旅行史和接触史','本人或共同居住人是否有以下症状未痊愈','是否从省外返鲁','是否从省内其他地级市返青','今日体温范围','上报日期');
        $title = array('序号','姓名','部门','手机号','单位','省份','城市', '区县','微信定位地址','当天进入校区','当天是否进行核酸检测','人员类别','本人或共同居住人是否有下列情况','本人或共同居住人是否有下列情况','本人或共同居住人是否有以下症状未痊愈','本人或共同居住人（7天内）是否有疫情重点地区（包括境外、国内中高低风险地区等）旅居史或接触史','是否从省外返鲁','省外地级市地址','是否从省内其他地级市返青','省内地级市地址','今日体温范围');

        foreach($title as $k => $v) {
            $title[$k] = iconv('UTF-8', 'GBK', $v);
        }

        fputcsv($fp, $title);

        $i = 1;
        foreach($rows as $val){
            $row[0] = iconv('UTF-8', 'GBK', $i++);
            $row[1] = mb_convert_encoding(trim($val['nickname']), 'GBK', 'UTF-8');
            $row[2] = iconv('UTF-8', 'GBK', $val['college_name']);
            $row[3] = iconv('UTF-8', 'GBK', $val['tel']);
            $row[4] = iconv('UTF-8', 'GBK', $val['classes_name']);

            $row[5] = iconv('UTF-8', 'GBK', $val['province']);
            $row[6] = iconv('UTF-8', 'GBK', $val['city']);
            $row[7] = iconv('UTF-8', 'GBK', $val['district']);
            $row[8] = iconv('UTF-8', 'GBK', $val['wx_formatted_address']);

            $row[9] = mb_convert_encoding(trim($val['is_into_text']), 'GBK', 'UTF-8');
            $row[10] = mb_convert_encoding(trim($val['is_nucleic_text']), 'GBK', 'UTF-8');
            $row[11] = mb_convert_encoding(trim($val['a_21_text']), 'GBK', 'UTF-8');

            $row[12] = mb_convert_encoding(trim($val['a_1_text']), 'GBK', 'UTF-8');
            $row[13] = mb_convert_encoding(trim($val['a_2_text']), 'GBK', 'UTF-8');
            $row[14] = mb_convert_encoding(trim($val['a_5_text']), 'GBK', 'UTF-8');
            $row[15] = mb_convert_encoding(trim($val['a_4_text']), 'GBK', 'UTF-8');

            $row[16] = mb_convert_encoding(trim($val['a_6_text']), 'GBK', 'UTF-8');
            $row[17] = mb_convert_encoding(trim($val['shengwai_other']), 'GBK', 'UTF-8');
            $row[18] = mb_convert_encoding(trim($val['a_7_text']), 'GBK', 'UTF-8');
            $row[19] = mb_convert_encoding(trim($val['sheng_other']), 'GBK', 'UTF-8');
            $row[19] = mb_convert_encoding(trim($val['a_9_text']), 'GBK', 'UTF-8');

            fputcsv($fp, $row);
        }
        // foreach($rows as $val){
        //     $row[0] = iconv('UTF-8', 'GBK', $i++);
        //     $row[1] = iconv('UTF-8', 'GBK', $val['nickname']);
        //     $row[2] = iconv('UTF-8', 'GBK', $val['tel']);
        //     $row[3] = iconv('UTF-8', 'GBK', $val['college_name']);
        //     $row[4] = iconv('UTF-8', 'GBK', $val['classes_name']);

        //     $row[5] = iconv('UTF-8', 'GBK', $val['province']);
        //     $row[6] = iconv('UTF-8', 'GBK', $val['city']);
        //     $row[7] = iconv('UTF-8', 'GBK', $val['district']);
        //     $row[8] = iconv('UTF-8', 'GBK', $val['wx_formatted_address']);
        //     $row[9] = iconv('UTF-8', 'GBK', $val['a_1']);
        //     $row[10] = iconv('UTF-8', 'GBK', $val['a_1_1']);
        //     $row[11] = iconv('UTF-8', 'GBK', $val['a_2']);
        //     $row[12] = iconv('UTF-8', 'GBK', $val['a_2_1']);
        //     $row[13] = iconv('UTF-8', 'GBK', $val['a_3']);
        //     $row[14] = iconv('UTF-8', 'GBK', $val['a_3_1']);
        //     $row[15] = iconv('UTF-8', 'GBK', $val['a_3_2']);
        //     $row[16] = iconv('UTF-8', 'GBK', $val['a_4']);
        //     $row[17] = iconv('UTF-8', 'GBK', $val['a_4_1']);
        //     $row[18] = iconv('UTF-8', 'GBK', $val['a_5']);
        //     $row[19] = iconv('UTF-8', 'GBK', $val['a_5_1']);

        //     $row[20] = iconv('UTF-8', 'GBK', $val['a_6']);
        //     if($val['a_6'] == 0){
        //         $row[21] = iconv('UTF-8', 'GBK', '-');
        //     }else{
        //         $row[21] = iconv('UTF-8', 'GBK', $val['a_6_1']);
        //     }
        //     $row[22] = iconv('UTF-8', 'GBK', $val['a_6_2']);

        //     $row[23] = iconv('UTF-8', 'GBK', $val['a_7']);
        //     $row[24] = iconv('UTF-8', 'GBK', $val['a_9']);
        //     if(isset($val['inoculation']))
        //     {
        //         if($val['inoculation']==1)
        //         {
        //             $row[25] = iconv('UTF-8', 'GBK', "已接种第一针");
        //         }else if($val['inoculation']==2){
        //             $row[25] = iconv('UTF-8', 'GBK', "已接种第二针");
        //         }else if($val['inoculation']==3){
        //             $row[25] = iconv('UTF-8', 'GBK', "已接种第三针");
        //         }else
        //             $row[25] = iconv('UTF-8', 'GBK', "尚未接种");
        //     }else
        //         $row[25] = "-";


        //     $row[26] = iconv('UTF-8', 'GBK', $val['create_time']);

        //     fputcsv($fp, $row);
        // }
        ob_flush();
        flush();
    }

     public function exp_inoculation(){
        $and = ' ';
        $gid = $this->auth->gid;
        //学院
        if($this->auth->gtypeid == 1){
            $and .= " and college_id = '$gid' ";
        }
        //班级
        if($this->auth->gtypeid == 3){
            exit('没有权限!');
        }

        $create_time = input('create_time');
        if(empty($create_time)){
            $timestampArr = Time::today();
            $starttime = strtotime($timestampArr[0]);
            $endtime = strtotime($timestampArr[1]);

        }else{
            $timeArr = explode(' - ',$create_time);
            $starttime = strtotime($timeArr[0]);
            $endtime = strtotime($timeArr[1]);
        }

        $province = input('province');
        $city = input('city');



        if(!empty($starttime)){
            $and .= " and create_time >='$starttime' ";
        }
        if(!empty($endtime)){
            $and .= " and create_time <= '$endtime' " ;
        }

        if(!empty($province)){
            $and .= " and province='$province' ";
        }
        if(!empty($city)){
            $and .= " and city='$city' ";
        }
        if($this -> whereType){
            $and .= " and type=".$this -> whereType['type'];
        }

        $sql = "select nickname,tel,classes_name,college_name,province,city,district, wx_formatted_address,inoculation,
            case a_1 when '0' then '无情况'  when '1' then '途径武汉' when '2' then '途径湖北(除武汉)' when '3' then '居住武汉' when '4' then '居住湖北(除武汉)' when '5' then '接触武汉籍人员'  when '6' then '接触湖北(除武汉)籍人员' when '7' then '从外省返鲁' when '8' then '在境外停留或途经' else '-' end a_1,
            case a_1_1 when 0 then '无' else from_unixtime(a_1_1, '%Y-%m-%d') end a_1_1,
            case a_2 when '0' then '无情况'  when '1' then '接触确诊病历' when '2' then '接触疑似病历' when '3' then '接触无症状感染者' else '-' end a_2,
            case a_2_1 when 0 then '无' else from_unixtime(a_2_1, '%Y-%m-%d') end a_2_1,
            case a_3 when '0' then '无' when '1' then '医院隔离' when '2' then '当地指定点隔离' when '3' then '居家隔离' else '-' end a_3,
            case a_3_1 when 0 then '无' else from_unixtime(a_3_1, '%Y-%m-%d') end a_3_1,
            case a_3_2 when '0' then '否' when '1' then '是' else '-' end a_3_2,
            case a_4 when '0' then '健康'  when '1' then '咳嗽' when '2' then '发烧' when '3' then '呼吸困难等' when '4' then '确诊病历' when '5' then '疑似病历'  when '6' then '其它' when '7' then '咽痛、乏力、肌肉酸痛、胸闷、恶心呕吐、腹泻、结膜炎' else '-' end a_4,
            a_4_1,
            case a_5 when '0' then '无情况'  when '1' then '接触确诊病历' when '2' then '接触疑似病历' when '3' then '确诊病历' when '4' then '疑似病历' when '5' then '有境外返回' else '-' end a_5,
            case a_5_1 when 0 then '无' else from_unixtime(a_5_1, '%Y-%m-%d') end a_5_1,
            case a_6 when '0' then '未返程' when '1' then '返程' else '-' end a_6,
            case a_6_1 when '0' then '自驾' when '1' then '火车' when '2' then '飞机' when '3' then '长途车' else '-' end a_6_1,
            a_6_2,
            case a_7 when '0' then '没有' when '1' then '因值班进入' when '2' then '因其他事宜进入' else '-' end a_7,
            case a_9 when '0' then '36度以下' when '1' then '36-36.5度' when '2' then '36.6-36.9度' when '3' then '37-37.2度' when '4' then '37.3-37.9度' when '5' then '38-38.5度' when '6' then '38.6-38.9度' when '7' then '39-40度' when '8' then '40度以上' end a_9,
            case create_time when 0 then '无' else from_unixtime(create_time, '%Y-%m-%d') end create_time
            from qa_ques_teacher where inoculation>0 $and";

        $rows = Db::query($sql);

        $fp = fopen('php://output', 'a+');
        header("Content-Type:application/vnd.ms-excel;charset=utf-8");
        header("Content-Disposition:filename=教职工上报数据导出.csv");

        $title = array('序号','姓名','手机号','部门','单位','省份','城市', '区县',
            '微信定位地址','你最近是否有下属情况','填写时间','近期接触相关人员情况','接触病例时间','是否医学观察',
            '医学观察时间','是否结束隔离','你本人身体近况','身体状况说明','家庭成员近期','家庭成员接触病例时间','是否有返程计划','返程工具','车次','今日有无进入校园','体温范围','疫苗接种情况','上报日期');
        foreach($title as $k => $v) {
            $title[$k] = iconv('UTF-8', 'GBK', $v);
        }

        fputcsv($fp, $title);

        $i = 1;
        foreach($rows as $val){
            $row[0] = iconv('UTF-8', 'GBK', $i++);
            $row[1] = iconv('UTF-8', 'GBK', $val['nickname']);
            $row[2] = iconv('UTF-8', 'GBK', $val['tel']);
            $row[3] = iconv('UTF-8', 'GBK', $val['college_name']);
            $row[4] = iconv('UTF-8', 'GBK', $val['classes_name']);

            $row[5] = iconv('UTF-8', 'GBK', $val['province']);
            $row[6] = iconv('UTF-8', 'GBK', $val['city']);
            $row[7] = iconv('UTF-8', 'GBK', $val['district']);
            $row[8] = iconv('UTF-8', 'GBK', $val['wx_formatted_address']);
            $row[9] = iconv('UTF-8', 'GBK', $val['a_1']);
            $row[10] = iconv('UTF-8', 'GBK', $val['a_1_1']);
            $row[11] = iconv('UTF-8', 'GBK', $val['a_2']);
            $row[12] = iconv('UTF-8', 'GBK', $val['a_2_1']);
            $row[13] = iconv('UTF-8', 'GBK', $val['a_3']);
            $row[14] = iconv('UTF-8', 'GBK', $val['a_3_1']);
            $row[15] = iconv('UTF-8', 'GBK', $val['a_3_2']);
            $row[16] = iconv('UTF-8', 'GBK', $val['a_4']);
            $row[17] = iconv('UTF-8', 'GBK', $val['a_4_1']);
            $row[18] = iconv('UTF-8', 'GBK', $val['a_5']);
            $row[19] = iconv('UTF-8', 'GBK', $val['a_5_1']);

            $row[20] = iconv('UTF-8', 'GBK', $val['a_6']);
            if($val['a_6'] == 0){
                $row[21] = iconv('UTF-8', 'GBK', '-');
            }else{
                $row[21] = iconv('UTF-8', 'GBK', $val['a_6_1']);
            }
            $row[22] = iconv('UTF-8', 'GBK', $val['a_6_2']);

            $row[23] = iconv('UTF-8', 'GBK', $val['a_7']);
            $row[24] = iconv('UTF-8', 'GBK', $val['a_9']);
            if(isset($val['inoculation']))
            {
                if($val['inoculation']==1)
                {
                    $row[25] = iconv('UTF-8', 'GBK', "已接种第一针");
                }else if($val['inoculation']==2){
                    $row[25] = iconv('UTF-8', 'GBK', "已接种第二针");
                }else if($val['inoculation']==3){
                    $row[25] = iconv('UTF-8', 'GBK', "已接种第三针");
                }else
                    $row[25] = iconv('UTF-8', 'GBK', "尚未接种");
            }else
                $row[25] = "-";


            $row[26] = iconv('UTF-8', 'GBK', $val['create_time']);

            fputcsv($fp, $row);
        }
        ob_flush();
        flush();
    }


}
